<template>
  <div>
    <div>
      <el-table :data="tableData" style="width: 100%" :default-sort="{ prop: 'date', order: 'descending' }">
        <el-table-column prop="plaId" label="序号" width="270"></el-table-column>
        <el-table-column prop="plaName" label="姓名" width="270"></el-table-column>
        <el-table-column prop="plaAvatar" label="头像" width="270"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="startscore(scope.row)">开始打分</el-button>
            <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
              <el-button slot="reference" type="danger" style="margin-left: 5px">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <!-- Form -->
      <el-dialog title="请填写信息" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="form" ref="formRef">
          <el-form-item label="分数" label-width="15%">
            <el-input v-model="form.recScore" placeholder="请输入非数字内容" autocomplete="off" style="width: 90%"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request';

export default {
  data() {
    return {
      dialogFormVisible: false,
      form: {},
      tableData: []
    };
  },
  created() {
    this.load();
  },
  methods: {
    handleAvatarSuccess(res, file) {
      request.get(`/common/download?name=${res.content}`).then(response => {
        this.form.plaAvatar = JSON.parse(response.data).url;
      }).catch(error => {
        console.error('下载头像失败', error);
      });
    },
    load() {
      request.get("/player/list").then(res => {
        if (res.code === 1) {
          this.tableData = res.data;
        } else {
          console.error('加载数据失败', res.message);
        }
      }).catch(error => {
        console.error('网络请求失败', error);
      });
    },
    startscore(row) {
      this.form = { ...row };
      this.dialogFormVisible = true;
    },
    del(id) {
      request.delete(`/pla/delete/${id}`).then(res => {
        if (res.code === 1) {
          this.$message.success('删除成功');
          this.load();
        } else {
          this.$message.error('删除失败');
        }
      }).catch(error => {
        console.error('删除请求失败', error);
      });
    },
    submit() {
      const userData = localStorage.getItem('user');
      const token = userData ? JSON.parse(userData).token : '';
      if (this.form.id) {
        // 更新数据
        request.put(`/record/score`, this.form).then(res => {
          if (res.code === 1) {
            this.$message.success('更新成功');
            this.load();
            this.dialogFormVisible = false;
          } else {
            this.$message.error('更新失败');
          }
        }).catch(error => {
          console.error('更新请求失败', error);
        });
      } else {
        // 新增数据
        request.post('/pla/add', this.form).then(res => {
          if (res.code === 1) {
            this.$message.success('添加成功');
            this.load();
            this.dialogFormVisible = false;
          } else {
            this.$message.error('添加失败');
          }
        }).catch(error => {
          console.error('添加请求失败', error);
        });
      }
    }
  }
};
</script>

<style scoped>
.el-table {
  margin-top: 20px;
}

.el-dialog {
  text-align: center;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-button {
  margin: 5px;
}
</style>